package com.erp.erp_mask.controller;


import com.erp.erp_mask.mapper.TPartwareMapper;
import com.erp.erp_mask.pojo.TPartware;
import com.erp.erp_mask.service.TPartwareService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import net.sf.jasperreports.engine.JasperRunManager;
import org.apache.ibatis.io.Resources;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.sql.DataSource;
import java.io.InputStream;

@RestController
@RequestMapping("/bas")
public class TPartwareController {

    @Resource
    private TPartwareService tPartwareService;
    @Resource
    private TPartwareMapper tPartwareMapper;

    @GetMapping("/index")
    public ModelAndView index(){
        return new ModelAndView("/bas/Tware");
    }

    @GetMapping("/selectList")
    public Result select(String name,Long pageNo,Long pageSize){
        return Result.success(tPartwareService.searchware(name,pageNo,pageSize));
    }

    @Resource
    private DataSource dataSource;
    @SneakyThrows
    @GetMapping("/exportPdf")
    public void exportPdf( HttpServletResponse response){
        response.setContentType("application/pdf");
        InputStream in= Resources.getResourceAsStream("muban/ljcangku.jasper");
        JasperRunManager.runReportToPdfStream(in,response.getOutputStream(),null,dataSource.getConnection());
    }
    @GetMapping("/lsearchnum")
    public Result lsearchnum(String name){
        return Result.success(tPartwareMapper.lsearchnum(name));
    }

    @PostMapping("/updateware")
    public Result update(@RequestBody TPartware tPartware){
        try{
            tPartwareService.updateById(tPartware);
            return Result.success("修改成功！");
        }catch (Exception e){
            return Result.fail("修改失败！");
        }
    }

    @DeleteMapping("/delware")
    public Result del(Integer wareId){
        try {
            tPartwareService.removeById(wareId);
            return Result.success("删除成功！");
        }catch (Exception e){
            return Result.fail("删除失败！");
        }
    }



}
